var WxServer = {

    addListener() {
    },

    // wxLogin(ret_handler = null) {
    //     wx.login({
    //         success: function (res) {
    //             if (ret_handler == null) return;
    //             ret_handler(res);
    //         },
    //         fail: function (res) {
    //             console.log("login fail", res);
    //         }
    //     });
    // },
    wxLogin(ret_handler = null, fail_handle = null) {
        if (!CC_WECHATGAME) {
            return;
        }
        wx.login({
            success: function (res) {
                if (ret_handler == null) return;
                ret_handler(res);
            },
            fail: function (res) {
                console.log("login fail", res);
                if (fail_handle == null) return;
                fail_handle(res);
            }
        });
    },
    //创建授权按钮
    createUserInfoButton(left_x, top_y, w, h, url, ret_handler = null) {
        if (!CC_WECHATGAME) {
            return;
        }
        var wxBtn = wx.createUserInfoButton({
            type: 'image',
            text: '获取用户信息',
            image: url,
            withCredentials: true,       //是否带上登录态信息。当 withCredentials 为 true 时，要求此前有调用过 wx.login 且登录态尚未过期，此时返回的数据会包含 encryptedData, iv 等敏感信息；当 withCredentials 为 false 时，不要求有登录态，返回的数据不包含 encryptedData, iv 等敏感信息。
            style: {
                left: left_x,
                top: top_y,
                width: w,
                height: h,
            }
        });

        wxBtn.onTap((res) => {
            if (ret_handler == null) return;
            ret_handler(res, wxBtn);
        });
    },

    wxGetSetting(ret_handler = null, fail_handle = null, complete_handle) {
        if (!CC_WECHATGAME) {
            return;
        }
        wx.getSetting({
            success(res) {
                if (ret_handler == null) return;
                ret_handler(res);
            },
            fail(res) {
                if (fail_handle == null) return;
                fail_handle(res);
            },
            complete(res) {
                if (complete_handle == null) return;
                complete_handle(res);
            }
        })
    },



    // 向子域发送消息
    wxPostMessage(key_value) {
        if (!CC_WECHATGAME) {
            return;
        }
        wx.postMessage(key_value);
    },

    // 监听主域发来的消息
    wxOnMessage(ret_handler = null) {
        if (!CC_WECHATGAME) {
            return;
        }
        wx.onMessage(data => {
            if (ret_handler == null) return;
            ret_handler(data);
        });
    },

    //用户的 openid string avatarUrl用户的微信头像 url nickname用户的微信昵称
    getUserInfo(ret_handler = null) {
        if (!CC_WECHATGAME) {
            return;
        }
        wx.getUserInfo({
            openIdList: ['selfOpenId'],
            lang: 'zh_CN',
            success: (res) => {
                if (ret_handler == null) return;
                ret_handler(res);
            },
            fail: (res) => {
                console.log(res);
            },
            complete: (res) => {
            },
        });
    },

    getFriendCloudStorage(ret_handler = null) {
        if (!CC_WECHATGAME) {
            return;
        }
        wx.getFriendCloudStorage({
            success: function (res) {

            },
            fail: function (res) {
                console.error(res);
            },
            complete(res) {
                if (ret_handler == null) return;
                ret_handler(res.data);
            }
        });
    },

    getFriendCloudStorageByKeyList(keyArr, ret_handler = null) {
        if (!CC_WECHATGAME) {
            return;
        }
        wx.getFriendCloudStorage({
            keyList: keyArr,
            success: function (res) {
                if (ret_handler == null) return;
                ret_handler(res.data);
            },
            fail: function (res) {
                console.error(res);
            },
            complete(res) {
            }
        });
    },

    getGroupCloudStorage(ret_handler = null) {
        if (!CC_WECHATGAME) {
            return;
        }
        wx.getGroupCloudStorage({
            success: function (res) {

            },
            fail: function (res) {
                console.error(res);
            },
            complete(res) {
                if (ret_handler == null) return;
                ret_handler(res);
            }
        });
    },

    getUserCloudStorage(keyArr, ret_handler = null) {
        if (!CC_WECHATGAME) {
            return;
        }
        wx.getUserCloudStorage({
            keyList: keyArr,
            success: function (res) {

            },
            fail: function (res) {
                console.error(res);
            },
            complete(res) {
                if (ret_handler == null) return;
                ret_handler(res.KVDataList);
            }
        });
    },

    setUserCloudStorage(key_value_arr, ret_handler = null) {
        if (!CC_WECHATGAME) {
            return;
        }
        wx.setUserCloudStorage({
            KVDataList: key_value_arr,
            success: function (res) {

            },
            fail: function (res) {
                console.error(res);
            },
            complete(res) {
                if (ret_handler == null) return;
                ret_handler(res);
            }
        });
    },

    removeUserCloudStorage(key_arr, ret_handler = null) {
        if (!CC_WECHATGAME) {
            return;
        }
        wx.removeUserCloudStorage({
            keyList: key_arr,
            success: function (res) {

            },
            fail: function (res) {
                console.error(res);
            },
            complete(res) {
                if (ret_handler == null) return;
                ret_handler(res);
            }
        });
    },

    // 返回值res
    wxOnShow(ret_handler) {
        if (!CC_WECHATGAME) {
            return;
        }
        wx.onShow(ret_handler);
    },

    wxOnHide(ret_handler) {
        if (!CC_WECHATGAME) {
            return;
        }
        wx.onHide(ret_handler);
    },

    //获取敏感信息   比如 openId
    getWxSensitiveInfo(self, iv, encryptedData) {
        if (!CC_WECHATGAME) {
            return;
        }
        // var mSecret = "4486a5fab27b50a321cce385700780ec";
        // var mAppid = "wx0d194765140b8059";
        // var mUrl = "https://api.weixin.qq.com/sns/jscode2session?appid="+mAppid+"&secret="+mSecret+"&js_code="+self.m_code+"&grant_type=authorization_code";
        var mUrl = "https://xianqiju.cn/open/openid?code=" + this.m_code;
        wx.request({
            url: mUrl,
            data: { code: self.m_code },    //参数为键值对字符串
            success: function (res) {
                console.log("-----------request-----------");
                // var sessionKey = res.data.session_key;
                // var openid = res.data.openid;

                //解密 得到 用户信息
                // const desessionkey = base64.parse(sessionKey);
                // const deencryptedData = base64.parse(encryptedData);
                // const deiv = base64.parse(iv);
                // let decrypt = crypto.AES.decrypt(deencryptedData, desessionkey, {
                //     iv: deiv,
                //     mode: crypto.mode.CBC,
                //     padding: crypto.pad.Pkcs7
                // });
                // let decryptedStr = decrypt.toString(crypto.enc.Utf8);
                // console.log(JSON.stringify(decrypt));   
            }
        });
    },

    // 分享
    // icon 图片
    // query: "openId=" + window.data.open.openId + "&nick=" + nick + "&gender=" + gender + "&city=" + city + "&headUrl=" + headUrl,  分享需要带的参数
    shareAppMessage(mtitle, icon, query = "", ret_handler = null) {
        if (!CC_WECHATGAME) {
            return;
        }
        wx.shareAppMessage
            ({
                title: mtitle,
                imageUrl: icon,
                query: query,
                success: function success(res) {
                    wx.showShareMenu({
                        // 要求小程序返回分享目标信息
                        withShareTicket: true
                    });
                    console.log("分享成功", res);
                    if (ret_handler == null) return;
                    ret_handler(res);
                },
                fail: function fail(res) {
                    console.log("分享失败", res);
                }
            });
    },
}


module.exports = WxServer;